﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace DTO
{
    public class Message
    {
        int id;
        String from;
        String to;
        DateTime time;
        String content;

        public Message()
        {
        }

        /// <summary>
        /// Request ID.
        /// </summary>
        public int ID
        {
            get { return this.id; }
        }
           /// <summary>
        /// Create DTO.Message instance using SqlDataReader object.
        /// </summary>
        /// <param name="reader">Object contains informations get from DB.</param>
        public Message(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                 id = reader.GetInt32(0);
                 from = reader.GetString(1);
                 to = reader.GetString(2);
                 time = reader.GetDateTime(3);
                 content = reader.GetString(4);
              }
        }
        /// <summary>
        /// ID of the user who send the message.
        /// </summary>
        public String From
        {
            get { return this.from; }
            set 
            {
                if (value == null || "".Equals(value))
                    throw new Exception("ID of the user who send message is null.");
                else
                    this.from = value;
            }
        }

        /// <summary>
        /// ID of the user who receive the message.
        /// </summary>
        public String To
        {
            get { return this.to; }
            set
            {
                if (value == null || "".Equals(value))
                    throw new Exception("ID of the user who receive message is null.");
                else
                    this.to = value;
            }
        }

        /// <summary>
        /// The time when this message is sent.
        /// </summary>
        public DateTime Time
        {
            get { return this.time; }
            set
            {
                if (value == null)
                    throw new Exception("Time sending message is null.");
                else
                    this.time = value;
            }
        }

        /// <summary>
        /// Content of message.
        /// </summary>
        public String Content
        {
            get { return this.content; }
            set
            {
                if (value == null)
                    throw new Exception("Content of this message is null.");
            }
        }
    }
}